package com.bhs.sansonglogistics.ui.message;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.CircleMessage;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.FastCilckOneUtil;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 圈子消息
 */
public class CircleFriendsMessageActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private int page = 1;
    private BaseQuickAdapter<CircleMessage.DataDTO.ListDTO, BaseViewHolder> mAdapter;


    @Override
    public int initLayout() {
        return R.layout.activity_circle_friends_message;
    }

    @Override
    public void initView() {
        findViewById(R.id.tv_clear).setOnClickListener(this);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<CircleMessage.DataDTO.ListDTO, BaseViewHolder>(R.layout.item_circle_message) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, CircleMessage.DataDTO.ListDTO item) {
                Glide.with(mContext).load(item.getHeaderpic()).into((ImageView) helper.getView(R.id.iv_face));
                helper.setText(R.id.tv_nick_name, item.getName());
                helper.setText(R.id.tv_comment, item.getWords());
                helper.setText(R.id.tv_time, MyUtils.getBefore(item.getCreate_time()));
                Glide.with(mContext).load(item.getResource()).into((ImageView) helper.getView(R.id.iv_cover));
                helper.setGone(R.id.iv_video, item.getResource().endsWith(".mp4"));
                helper.setGone(R.id.tv_comment, item.getAction_type() == 1);
                helper.setGone(R.id.iv_praise, item.getAction_type() == 2);
            }
        };

        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        loadData();

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null));

    }

    private void loadData() {
        networkRequest(netApi.circleNewsList(page, 10), this);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            CircleMessage dataBean = new Gson().fromJson(json, CircleMessage.class);
            mRefreshLayout.setRefreshing(false);
            if (dataBean.getCode().equals("1")) {
                if (page == 1) {
                    mAdapter.setNewData(dataBean.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(dataBean.getData().getList());
                }
                if (mAdapter.getData().size() >= dataBean.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            } else {
                ToastUtil.show(dataBean.getMsg());
            }
        } else {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            if (bean.getCode().equals("1")) {
                loadingPopupDismiss();
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
            } else {
                ToastUtil.show(bean.getMsg());
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }

    @Override
    public void onClick(View view) {
        if (FastCilckOneUtil.isFastClick()) {
            return;
        }
        loadingPopupShow();
        networkRequest(netApi.circleDelNews(), this, 555);
    }
}